<template>
  <div class="login_container">
    <div class="bg"></div>
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img src="@/assets/user.png" alt="">
      </div>
      <!-- 登录表单区域 -->
      <el-form :v-model="login_from" class="login_from">
        <div class="user" prop="username">
          <input type="text" placeholder="Full Name" name="username" id="username" v-model="login_from.username">
        </div>
        <div class="pass">
          <input type="password" placeholder="password" name="password" id="password" v-model="login_from.password">
        </div>
        <button @click.prevent="login">登录</button>
      </el-form>
    </div>

  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      //这是登录表单的数据绑定对象
      login_from: {
        username: '',
        password: ''
      }
    }
  },
  methods: {
    //登录验证并发送请求
    async login() {
      const { data: res } = await this.$http.post('login', this.login_from)
      if (res.meta.status !== 200) return this.$message.error('登录失败')
      this.$message.success('登录成功!')
      //1.将登录成功之后的token,保存到客户端的sessionStorage中
      //1.1项目中出了登录之外的其他API接口，必须在登录之后才能访问
      //1.2token只应在当前网站打开期间生效，所以将token保存在sessionStorage中
      window.sessionStorage.setItem('token', res.data.token)
      //2.通过编程式导航跳转至后台主页，路由地址是/home
      this.$router.push('/home')
    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
  position: relative;
  height: 100%;
  background-color: #b1d9db;
  .bg {
    position: absolute;
    left: 122px;
    top: 167px;
    width: 389.57px;
    height: 405.59px;
    background-image: url('@/assets/Abstraction.png');
  }
  .login_box {
    position: absolute;
    right: 200px;
    top: 150px;
    width: 426px;
    height: 368px;
    border-radius: 5%;
    background-color: #fff;
    form {
      display: flex;
      justify-content: center;
      .user {
        width: 345px;
        position: absolute;
        height: 40px;
        border-bottom: 1px solid rgba(196, 196, 196, 1);
        transform: rotate(-0.17629410326480865deg);
        top: 30%;
        #username {
          background-color: #fff;
          width: 345.19px;
          border: none;
          height: 40px;
        }
      }
      .pass {
        width: 345px;
        position: absolute;
        height: 40px;
        border-bottom: 1px solid rgba(196, 196, 196, 1);
        transform: rotate(-0.17629410326480865deg);
        top: 55%;
        #password {
          background-color: #fff;
          width: 345.19px;
          border: none;
          height: 40px;
        }
      }
      button {
        /*rectangle 4*/
        position: absolute;
        width: 345px;
        height: 30px;
        background: rgba(167, 204, 206, 1);
        border: 1px solid rgba(167, 204, 206, 1);
        bottom: 10%;
        color: #fff;
        border-radius: 10px;
      }
    }

    .avatar_box {
      position: absolute;
      width: 100px;
      height: 100px;
      border: 1px solid #eee;
      border-radius: 50%;
      box-shadow: 0 0 10px #ddd;
      left: 50%;
      transform: translate(-50%, -50%);
      background-color: #fff;
      img {
        position: absolute;
        top: 10px;
        left: 15px;
        width: 70px;
        border-radius: 40%;
      }
    }
  }
}
</style>